package com.vsen.lpr.service;

import com.google.api.client.util.Lists;
import com.vsen.lpr.common.model.Response;
import com.vsen.lpr.common.utils.BeanCopyUtil;
import com.vsen.lpr.common.utils.MinioTemplate;
import com.vsen.lpr.entity.Banner;
import com.baomidou.mybatisplus.service.IService;
import com.vsen.lpr.mapper.BannerMapper;
import com.vsen.lpr.pojo.dto.BannerDto;
import com.vsen.lpr.pojo.dto.InsertBannerDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author yy
 * @since 2021-09-01
 */
@Service
public class BannerService  {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private MinioTemplate minioTemplate;

    public Response insertBanner(InsertBannerDto dto) {
        Banner banner = new Banner();
        BeanCopyUtil.copyFromSrcToDest(dto,banner);
        banner.setType(0);//首页轮播图默认0
        bannerMapper.insert(banner);
        return Response.success();
    }

    public Response<List<String>> bannerList(BannerDto dto) {
        dto.setUserId("48184");
        List<String> resultList = Lists.newArrayList();
        List<String> list = bannerMapper.selectListByUserId(dto.getUserId());

        for(String s : list){
            resultList.add(minioTemplate.getObjectURL("lpr",s));
        }

        while (resultList.size()>4){
            int size = resultList.size();
            int random = (int) (Math.random()*100%size);
            resultList.remove(random);
        }

        return Response.success(resultList);
    }
}
